<?php

error_reporting(E_ALL);
$relPath='./pinc/';
require_once $relPath."dpinit.php";
require_once $relPath."dp_main.inc";
// require_once $relPath."postfiles.php";

$User->IsLoggedIn()
    or RedirectToLogin();

$projectid      = ArgProjectId();
if($projectid == "")
    die("Requires project id.");

$roundid        = ArgRoundId();
$isheaders      = Arg("isheaders");
$isnames        = Arg("isnames");
$dlconcat       = IsArg("dlconcat");
$dlpages        = IsArg("dlpages");
$dlimages       = IsArg("dlimages");

// ---------------------------------------------------------

$project    = new DpProject($projectid);
$pagecount  = $project->PageCount();
$imagecount = $project->ImageCount();

$pagecount + $imagecount > 0
    or die("Nothing to download.");

if($dlconcat && $pagecount > 0) {
    DownloadConcat($project, $roundid, $isheaders, $isnames);
}
else if($dlpages && $pagecount > 0) {
    DownloadPages($project, $roundid, $isheaders, $isnames);
}
else if($dlimages && $imagecount > 0) {
    DownloadImages($project);
}

dp_header("Download ".$project->Title());

echo $project->TitleAuthor();

echo "
<div class='padded'>
    <form method='post' accept-charset='UTF-8' action=''>
        <input type='hidden' name='projectid' value='$projectid'>
        <table>
            <tr><td>
            ".link_to_project($projectid, "Return to project page")."
            </td></tr>
            <tr><td>
            <input type='radio' name='roundid' value='OCR'> OCR<br>\n";
foreach(RoundIdsInOrder() as $roundid) {
    echo "
            <input type='radio' name='roundid'
                        value='$roundid'>$roundid<br>\n";
    if($roundid == $project->RoundId()) {
        break;
    }
}
if($pagecount > 0) {
    echo "
            <input type='radio' name='roundid' value='latest'
                                    checked='checked'>Latest<br>
            </td>
            <td class='left'>
                <input type='checkbox' name='isheaders' id='isheaders'
                                        checked='checked'>
        ". _("Page headers with filenames")."<br>\n";
    if($project->UserMaySeeNames()) {
        echo "
                <input type='checkbox' name='isnames' id='isnames'>
        ". _("Usernames in page headers");
    }
    echo "
            </td></tr>\n";
}
else {
    echo "<h4>No pages to download.</h4>
            </td></tr>\n";
}
echo "
            <tr><td colspan='2' class='center'>\n";
if($pagecount > 0) {
    echo "
    <input type='submit' name='dlconcat' value='Concatenated text'>
    <input type='submit' name='dlpages' value='One file per page'>\n";
}
if($imagecount > 0) {
    echo "
    <input type='submit' name='dlimages' value='Image files'>\n";
}
echo "
            </td></tr>\n";
echo "
            <tr><td colspan='2'>
    ". _("You will receive your data in one zip file.")."
            </td></tr>
        </table>
    </form>
    </div>
</div>\n";


theme_footer();
exit;



function DownloadPages($project, $roundid, $isheaders, $isnames) {
    global $Context;
    $filestub = "{$project->ProjectId()}_{$roundid}_pages_"
        . ($isnames ? "names" : "nonames");
    $pages = $project->ProjectPages();
    foreach($pages as $page) {
        $pagename = $page->PageName();
        $imgfile  = $page->ImageFileName();
        $pagetext = ($roundid == "latest")
                    ? $page->ActiveText()
                    : $page->RoundText($roundid);

        $pagetext = preg_replace("/\r/", "", $pagetext);
        if($isheaders) {
            $hdr = "-----File: {$imgfile} ";
            if($isnames) {
                $proofers = implode("\\", $page->ProofersArray());
                $hdr .= $proofers;
            }
            $hdr = str_pad($hdr, 74, "-", STR_PAD_RIGHT);
            $a[$pagename.".txt"] = "$hdr\n$pagetext\n";
        }
        else {
            $a[$pagename.".txt"] = "$pagetext\n";
        }
    }
    $Context->ZipSendArray($filestub, $a);
}

function DownloadConcat($project, $roundid, $isheaders, $isnames) {
    global $Context;
    $filestub = "{$project->ProjectId()}_{$roundid}_concat_"
        . ($isnames ? "names" : "nonames");
    if($roundid == "latest" && (! $isheaders)) {
        $text = $project->Text();
    }
    else {
        $text = "";
        $pages = $project->ProjectPages();
        foreach($pages as $page) {
            $pagename = $page->PageName();
            $imgfile  = $page->ImageFileName();
            $pagetext = ($roundid == "latest")
                        ? $page->ActiveText()
                        : $page->RoundText($roundid);

            if($isheaders) {
                $hdr = "-----File: {$imgfile}---";
                if($isnames) {
                    $proofers = implode("\\", $page->ProofersArray());
                    $hdr .= ("\\".$proofers."\\");
                }
                $hdr = str_pad($hdr, 74, "-", STR_PAD_RIGHT);
                $text .= "$hdr\n$pagetext\n";
            }
            else {
                $text .= "$pagetext\n";
            }
        }
    }
    $text = preg_replace("/\r/", "", $text)."\n";
    $Context->ZipSendString($filestub, $text);
}

function DownloadImages($project) {
    global $Context;
    $projectid = $project->ProjectId();
    $pages = $project->ProjectPages();
    $zipstub = "{$projectid}_images";

    $apaths = array();

    $ary = array();
    foreach($pages as $page) {
        if($page->IsImageFile()) {
            $apaths[] = $page->ImageFilePath();
        }
    }

    if(count($apaths) == 0) {
        return;
    }
    $Context->ZipSendFileArray($zipstub, $apaths);
}

// vim: sw=4 ts=4 expandtab
?>
